package com.example.day13_7_13_2;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class InfoAdapter extends BaseAdapter {
    private Context context;
    private List<Student> list;

    public InfoAdapter(Context context, List<Student> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_id.setText(list.get(position).getId()+"");
        holder.tv_name.setText(list.get(position).getName());
        holder.tv_age.setText(list.get(position).getAge()+"");
        return convertView;
    }

    public static
    class ViewHolder {
        public View rootView;
        public TextView tv_id;
        public TextView tv_name;
        public TextView tv_age;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.tv_id = (TextView) rootView.findViewById(R.id.tv_id);
            this.tv_name = (TextView) rootView.findViewById(R.id.tv_name);
            this.tv_age = (TextView) rootView.findViewById(R.id.tv_age);
        }

    }
}
